<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 2017/7/13
 * Time: 下午12:25
 */
namespace common\models;

use common\models\ar\Level;
use common\models\ar\MaxClientsn;
use common\models\ar\SysConfig;
use common\models\ar\User;

class Common
{
    /**
     * 生成推荐码
     *
     * @return string
     */
    public static function genRecommendCode()
    {
        $rand_arr = array(
            1 => array(65, 90),//大写字母
            2 => array(97, 122),//小写字母
            3 => array(0, 9),
        );

        $codeLen = 6;
        $return = "";
        for ($i = 1; $i <= $codeLen; $i++) {
            //先随机出是哪一类
            $tmpRandCat = mt_rand(1, 3);

            //随机数字
            $tmpRand = mt_rand($rand_arr[$tmpRandCat][0], $rand_arr[$tmpRandCat][1]);
            if ($tmpRandCat != 3) {
                //转换成相应的字符
                $tmpRand = chr($tmpRand);
            }
            $return .= $tmpRand;
        }

        //检测查看是否已存在
        $exist = User::find()->where('recommend_code = :recommend_code', [':recommend_code' => $return])->count();
        if ($exist) {
            return self::genRecommendCode();
        }

        return $return;
    }

    /**
     * 根据level id获取层级信息
     *
     * @param int $level
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getLevelsInfo($level = 0)
    {
             $lists =Level::find()->where(['cid' =>1])->asArray()->one();
            return $lists;
    }

    /**
     * 数据检测
     *
     * @param mixed $value 检测的数据
     * @param array $check 约束条件，min和max，对字符串来说表示字符长度，对数字来说表示数字大小
     *
     * @return array
     */
    public static function dataCheck(
        $value,
        $check = [
            'type' => 3,
            'min' => 6,
            'max' => 16,
            'detail' => [
                'pattern' => '/[a-zA-Z0-9_]{6,16}$/',
                'info' => '长度为6-16位字符串'
            ]
        ]
    ) {
        $return = ['code' => 1, 'msg' => 'ok'];
        switch ($check['type']) {
            case 1:
                //整型,用来判断范围是否符合
                if (!is_numeric($value) || !is_int($value + 0)) {
                    $return['code'] = 0;
                    $return['msg'] = "必须为整数类型";
                } else {
                    if ($value < $check['min'] || $value > $check['max']) {
                        $return['code'] = 0;
                        $return['msg'] = "大于等于" . $check['min'] . "，小于等于" . $check['max'] . "的整数";
                    }
                }
                break;
            case 2:
                //数值型,用来判断范围是否符合
                if (!is_numeric($value)) {
                    $return['code'] = 0;
                    $return['msg'] = "必须为数字类型";
                } else {
                    if ($value < $check['min'] || $value > $check['max']) {
                        $return['code'] = -2;
                        $return['msg'] = "大于等于" . $check['min'] . "，小于等于" . $check['max'] . "的数字";
                    }
                }
                break;
            case 3://字符型
                $len = strlen($value);
                if (isset($check['detail']) && isset($check['detail']['pattern'])) {
                    if (!preg_match($check['detail']['pattern'], $value)) {
                        $return['code'] = 0;
                        $return['msg'] = isset($check['detail']['info']) ? $check['detail']['info'] : 'unknown error';
                    }
                } else {
                    if ($check['min'] == $check['max'] && $len != $check['min']) {
                        $return['code'] = 0;
                        $return['msg'] = "长度为" . $check['max'] . "位的字符串";
                    } else {
                        if ($len < $check['min'] || $len > $check['max']) {
                            $return['code'] = 0;
                            $return['msg'] = "长度为" . $check['min'] . "-" . $check['max'] . "位的字符串";
                        }
                    }
                }
                break;
            case 4://布尔型
                if (!in_array($value, [0, 1]) && !in_array($value, [false, true])) {
                    $return['code'] = 0;
                    $return['msg'] = "不是有效的bool类型";
                }
                break;
        }

        return $return;
    }

    /**
     * 根据用户等级，生成相应的客户编号
     *
     * @param $level
     *
     * @return int
     */
    public static function getClientSN($level)
    {
        //获取当前该等级最大的客户编号
        $obj = MaxClientsn::find()->where(["level" => $level])->one();
        $clientSN = $obj->clientSN + 1;
        //越界
        if ($clientSN < $obj->min || $clientSN > $obj->max) {
            return 0;
        }

        //排除数字4
        $clientSN = (int)str_replace('4', '5', (string)$clientSN);
        //写到数据库中
        $obj->clientSN = $clientSN;
        $obj->save(false);
        return $clientSN;
    }

    /**
     * 系统配置参数
     */
    public static function sysConfig()
    {
        $tmp = SysConfig::find()->asArray()->all();
        $return = [];
        foreach ($tmp as $t) {
            $return[$t['key']] = $t;
        }

        return $return;
    }
}